<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {
	function __construct(){
		parent::__construct();
	}
	
	public function index(){
		$lastid=$this->input->get('lastid');

		$where=array();
		if(is_numeric($lastid)){
			$where['w.id <']=(int)$lastid;
		}
		$num=$this->checkLogin()?9:12;
		$this->Data_model->db->select('w.id,w.mid,w.title,w.puttime,w.shares,w.goodnum,w.viewnum,w.commnum,w.watchnum,w.image,w.ishome,w.iscomm,w.content,w.summary,m.mname,m.mnick')->from('works w')->join('member m','m.mid=w.mid','left');
		/*->join('member_data md','md.mid=w.mid','left')->group_by('w.id')*/
		$worklist = $this->Data_model->getData($where,'w.id DESC',$num,0);

		$ud=$this->getUnionData($worklist);
		foreach ($worklist as $key => $value) {
			if(!empty($ud['goods'][$value['id']])){
				$worklist[$key]['gooded']=$ud['goods'][$value['id']];
			}
			if(!empty($ud['watchs'][$value['id']])){
				$worklist[$key]['watched']=$ud['watchs'][$value['id']];
			}
		}

		foreach ($worklist as $key => $value) {
			$worklist[$key]['content']=empty($value['content'])?'未填写':$value['content'];
			//csubstr($value['content'],80);
			if($this->member->mid==$value['mid']){
				$worklist[$key]['href']=site_url('member/work/'.$value['id']);
				$worklist[$key]['spaceurl']=site_url('member');
			}else{
				$worklist[$key]['href']=site_url('space/work/'.$value['mid'].'/'.$value['id']);
				$worklist[$key]['spaceurl']=site_url('space/'.$value['mid']);
			}
			
			$worklist[$key]['puttime']=date('Y.m.d',$value['puttime']);
			if(empty($value['title'])){
				$worklist[$key]['title']='未填写';
			}
			if(empty($value['mnick'])){
				$worklist[$key]['mnick']=$value['mname'];
			}
		}
		
		
		$adinfo=$this->getAd('home');//广告信息
		if($this->input->is_ajax_request()){
			$tpl = $this->checkLogin()?'index_logined':'index_no';
			$this->view($tpl,array(
				'worklist'=>$worklist,
				'adinfo'=>$adinfo
				));
		}else{
			$where=array('m.iscomm'=>1);
			$this->Data_model->db->select('m.mid,m.mname,m.mnick,w.image,w.id,w.title,w.content')->from('member m')->join('works w','w.mid=m.mid','left')->group_by('m.mid');
			$toplist = $this->Data_model->getData($where,'w.puttime DESC,w.id DESC',10,0);
			$tpl = $this->cfg['site_home']==''?'home':$this->cfg['site_home'];
			$this->view($tpl,array(
				'worklist'=>$worklist,
				'toplist'=>$toplist,
				'adinfo'=>$adinfo,
				'latest'=>$this->getLatest()
				));
		}
	}

	
}